/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-29
 * Time: 19:38
 */
public class MyCircularQueue {
    private int[] elem;
    private int usedSize;
    private int front;
    private int rear;
    public MyCircularQueue(int k) {
        this.elem = new int[k];
    }

    //入队操作
    public boolean enQueue(int value) {
        if (this.usedSize == this.elem.length) {
            return false;
        }
        this.elem[this.rear] = value;
        this.usedSize++;
        this.rear = (this.rear + 1) % this.elem.length;
        return true;
    }

    //判断是否存放满数据
    public boolean isFull1() {
        return this.usedSize == this.elem.length;
    }
    public boolean isFull2() {
        return ((this.rear + 1) % this.elem.length) == this.front;
    }

    //出队
    public boolean deQueue() {
        if (this.usedSize == 0) {
            return false;
        }
        this.front = (this.front + 1) % this.elem.length;
        this.usedSize--;
        return true;
    }

    //判断是否有数据
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    //查看队头元素
    public int frontNum() throws RuntimeException {
        if (isEmpty()) {
            throw new RuntimeException("循环队列空");
        }
        int val = this.elem[this.front];
        return val;
    }

    //查看队尾元素
    public int RearNum() throws RuntimeException {
        if (isEmpty()) {
            throw new RuntimeException("空~");
        }
        if (this.rear == 0) {
            return this.elem[this.elem.length-1];
        }
        return this.elem[this.rear-1];
    }
}
